package com.sunday.fangdaifu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.model.DateType;
import com.sunday.fangdaifu.widgets.ShowBigImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/7/4.
 */

public class TaskDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<String> dataSet;
    private View.OnClickListener onClickListener;
    private int appWidth;

    public TaskDetailAdapter(Context context, List<String> dataSet) {
        mContext = context;
        this.dataSet = dataSet;
        appWidth = DeviceUtils.getDisplay(mContext).widthPixels;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task_detail_img, parent, false);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) v.getLayoutParams();
        layoutParams.width = appWidth * 5 / 7;
        layoutParams.height = appWidth * 15 / 28;
        v.setLayoutParams(layoutParams);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        String img = dataSet.get(position);
        Picasso.with(mContext)
                .load(img)
                .error(R.mipmap.ic_launcher)
                .resize(appWidth, appWidth * 3 / 4)
                .into(viewHolder.img);
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.img)
        ImageView img;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            img.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, ShowBigImage.class);
            intent.putStringArrayListExtra("data", (ArrayList<String>) dataSet);
            mContext.startActivity(intent);
        }
    }
}
